package com.example.dnfgamercommunitybackend.service.impl;

import com.example.dnfgamercommunitybackend.dao.AccountDao;
import com.example.dnfgamercommunitybackend.dao.PermissionDao;
import com.example.dnfgamercommunitybackend.entity.Account;
import com.example.dnfgamercommunitybackend.entity.LoginAccount;
import com.example.dnfgamercommunitybackend.util.RedisUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private AccountDao accountDao;
    @Resource
    private PermissionDao permissionDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = accountDao.getAccountByUsername(username);
        //未查到用户信息则抛出异常
        if (Objects.isNull(account)){
            throw new UsernameNotFoundException("用户名或密码错误");
        }else if(account.getStatus() == 2){
            throw new RuntimeException("账号为封禁状态");
        }else {
            List<String> list = permissionDao.getPermissionListByUserId(account.getId());
            //包装为UserDetails返回
            return new LoginAccount(account,list);
        }
    }
}
